import java.util.Arrays;

public class Main {
    public static void func(int[] array){
        int L = array.length;
        int cur = 0,dest = -1;
        //找最后一个复写值
        while(cur < L){
            if(array[cur]!= 0){
                dest++;
            }else{
                dest += 2;
            }
            if(dest >= L-1){
                break;
            }
            cur++;
        }
        //判断dest的位置
        if(dest == L){
            array[L-1] = 0;
            dest-=2;
            cur--;
        }
        while (cur >= 0){
            if(array[cur] == 0){
                array[dest--] = 0;
                array[dest--] = 0;
                cur--;
            }else{
                array[dest--] = array[cur--];
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {1,0,2,3,0,4};
        func(array);
        System.out.println(Arrays.toString(array));
    }
}